/**
 * 
 */
package com.zxc.alipay;

import java.util.Random;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundAuthOperationCancelModel;
import com.alipay.api.domain.AlipayFundAuthOperationDetailQueryModel;
import com.alipay.api.domain.AlipayFundAuthOrderFreezeModel;
import com.alipay.api.domain.AlipayFundAuthOrderUnfreezeModel;
import com.alipay.api.domain.AlipayFundAuthOrderVoucherCreateModel;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayFundAuthOperationCancelRequest;
import com.alipay.api.request.AlipayFundAuthOperationDetailQueryRequest;
import com.alipay.api.request.AlipayFundAuthOrderFreezeRequest;
import com.alipay.api.request.AlipayFundAuthOrderUnfreezeRequest;
import com.alipay.api.request.AlipayFundAuthOrderVoucherCreateRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayFundAuthOperationCancelResponse;
import com.alipay.api.response.AlipayFundAuthOperationDetailQueryResponse;
import com.alipay.api.response.AlipayFundAuthOrderFreezeResponse;
import com.alipay.api.response.AlipayFundAuthOrderUnfreezeResponse;
import com.alipay.api.response.AlipayFundAuthOrderVoucherCreateResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

/**
 * @author admin
 *
 */
public class FundAuthOrderFreezeProductionDemo {

	static String url = "https://openapi.alipay.com/gateway.do";
	
	static String appid = "2018082961179599";
	
	static String alipayLogonId = "guolv@fengxiangcloud.com";
	
	static String merchantid = "2088231500947368";
	
	static String merchantPrivate="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC8nVjcTZxHJ/FPcna5o2E8H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hcCiil0iKFaJhLsk18JNE4dtk529uTzsp6wxBa5BEVGnGBkeVXHNCuFZ/C+gLq35W5JqjHobn9lrlS5t3ezNhlwW9+1lCoCPN30XluT/zflfhQhQihAoGAL63zviPfaB7Iq29kpQsAFFi0wpKQXRYWrKhV5tVC0+msO6WD8+5mKRhEcD3IW8qOx4IoFLsfW0AIi1ZX7or98dTHplPkbXzuJ57OxpaI0eB2mkQnvA2jMBtLDkDhzgTuy7qi2vkLItTIZW6VMH50LjwKumG/o7MHKC573dNLTWkCgYBwBaCTO+rFDdOWGb0rjtT8vWW1IKMLY5onJ4Wu1qjMuHBwPQfay169jJIHyEYgvTuJnWRKHJtw0g5kKOUXkfgVA6s8E8LOqZuSsaPzBGxxFOS0suy9L95EyeZqb9y+9Q8mewB/GZzQilXCELb+X9uD2bB85kzH+w7p7iwQtnCSyA==";
	
	static String alipayPublic="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuexP+NyVuZzqQzHE+39K2tD+mkQwdmohM2zN0iv2aK+U9Fkl7EMeNaO6+pv0MJKCh1CWJxcojNC5sFlCsDyKpoTr00IgiuccbYbSOvNlmPOZr2pMug6RTtEMdJy/cGU32T6f2Px1gFFgr2d7wywL4jrl58avfFMP+jHHyVmEi5ULwMJzq98dng+WLRY/H+KyTD8jbIMu3oeYPoVsv3vWju48xMjFUWAccEEmhRCRFlWAftG2dnJTGOpF5CCQ5/pNtKfXh2L1u69pzU9HDzxt+GSTOWqWw4Z5aRGIKmal4S1WvXsYD1BEAYxETuZGWV1BHggBww8aARuaqoORFWsF/wIDAQAB";
	
	
	public static void main(String[] args) throws AlipayApiException {
//		fundAuthOrderVoucherCreate();
		
//		fundAuthQuery();
		
		fundAuthOrderUnFreeze();
		
//		fundAuthCancel();
		
//		testTradePay();
		
//		testTradeQuery();
		
//		testTradeRefund();
	}
	
	/**
	 * 转交易退款
	 * @throws AlipayApiException
	 */
	public static void testTradeRefund() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayTradeRefundModel model = new AlipayTradeRefundModel();
		model.setOutTradeNo("pay35632687135670284"); //与预授权转支付商户订单号相同，代表对该笔交易退款
		model.setRefundAmount("0.01");
		model.setRefundReason("预授权退款测试");	
		model.setOutRequestNo(getRefundNo());//标识一次退款请求，同一笔交易多次退款需要保证唯一，如部分退款则此参数必传。
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		request.setBizModel(model);
		AlipayTradeRefundResponse response = alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	/**
	 * 交易记录查询
	 * @throws AlipayApiException
	 */
	public static void testTradeQuery() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayTradeQueryModel model = new AlipayTradeQueryModel();
		model.setOutTradeNo("pay35632687135670284");
		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
		request.setBizModel(model);
		AlipayTradeQueryResponse response = alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	/**
	 * 转交易支付接口
	 * @throws AlipayApiException
	 */
	public static void testTradePay() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		AlipayTradePayModel model = new AlipayTradePayModel();
		model.setOutTradeNo(getPayTradeNo());      //预授权转支付商户订单号，为新的商户交易流水号
		model.setScene("bar_code");                // 固定值bar_code
		model.setProductCode("PRE_AUTH");          // 固定值PRE_AUTH
		model.setAuthNo("2018083010002001440262953367");  // 填写预授权冻结交易号
		model.setSubject("预授权转支付测试"); 	  // 解冻转支付标题，用于展示在支付宝账单中
		model.setTotalAmount("0.01");             // 结算支付金额
		model.setSellerId(merchantid);    // 填写卖家支付宝账户pid
		model.setBuyerId("2088002199564446");     // 2088202415071394填写预授权用户uid，通过预授权冻结接口返回的payer_user_id字段获取
		model.setBody("预授权解冻转支付测试");      	  // 可填写备注信息
		model.setTerminalId("test_terminal_id");      // 填写商户操作员编号
		model.setStoreId("test_store_id");   	// 填写实际交易发生的终端编号
		model.setTimeoutExpress("5m");             // 填写解冻转支付交易的超时时间
//			   ExtendParams extendParams = new ExtendParams();
//			    extendParams.setSysServiceProviderId("此处填写ISV签约返佣协议账户的PID为2088开头的16位数字");
//			    model.setExtendParams(extendParams);  //系统商开发需要传入正确的返佣参数才能拿到返佣,无返佣协议不需要传入该参数。
		model.setAuthConfirmMode("COMPLETE"); //自动解冻时取值COMPLETE,不传该参数默认剩余资金不会自动解冻NOT_COMPLETE。
		AlipayTradePayRequest request = new AlipayTradePayRequest();
		request.setBizModel(model);
		request.setNotifyUrl("https://www.tuduoduo.com/index.htm");
		AlipayTradePayResponse response = alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	/**
	 * 预授权撤销
	 * @throws AlipayApiException
	 */
	public static void fundAuthCancel() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayFundAuthOperationCancelRequest request = new AlipayFundAuthOperationCancelRequest();
		AlipayFundAuthOperationCancelModel model = new AlipayFundAuthOperationCancelModel();
		//model.setAuthNo("2017120110002001390206804295"); // 支付宝资金授权订单号，在授权冻结成功时返回参数中获得
		model.setOutOrderNo("con15360588335903750"); //商户的授权资金订单号，与支付宝的授权资金订单号不能同时为空
		//model.setOperationId("20171201317348823902"); //支付宝的授权资金操作流水号
		model.setOutRequestNo("crn15360588335903750");//与支付宝的授权资金操作流水号不能同时为空，与冻结流水号相同
		model.setRemark("预授权撤销"); // 商户对本次撤销操作的附言描述，长度不超过100个字母或50个汉字	
		request.setBizModel(model);
		AlipayFundAuthOperationCancelResponse response = alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	/**
	 * 预授权解冻
	 * @throws AlipayApiException
	 */
	public static void fundAuthOrderUnFreeze() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayFundAuthOrderUnfreezeRequest  request  =  new  AlipayFundAuthOrderUnfreezeRequest();
		AlipayFundAuthOrderUnfreezeModel  model  =  new  AlipayFundAuthOrderUnfreezeModel();
		model.setAuthNo("2018121410002001660287025101");  //  支付宝资金授权订单号，在授权冻结成功时返回需要入库保存
		model.setOutRequestNo(getUnFreezeNo());//同一商户每次不同的资金操作请求，商户请求流水号不能重复,且与与冻结流水号不同
		model.setAmount("50000.00");  //  本次操作解冻的金额，单位为：元（人民币），精确到小数点后两位
		model.setRemark("预授权解冻");  //  商户对本次解冻操作的附言描述，长度不超过100个字母或50个汉字	
		request.setBizModel(model);
		
		AlipayFundAuthOrderUnfreezeResponse  response  =  alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	/**
	 * 预授权查询
	 * @throws AlipayApiException
	 */
	public static void fundAuthQuery() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayFundAuthOperationDetailQueryRequest request = new AlipayFundAuthOperationDetailQueryRequest();
		
		AlipayFundAuthOperationDetailQueryModel model = new AlipayFundAuthOperationDetailQueryModel();
		//model.setAuthNo("2017120110002001390206804295"); // 支付宝资金授权订单号，在授权冻结成功时返回参数中获得
		model.setOutOrderNo("con15447743161551307"); //商户的授权资金订单号，与支付宝的授权资金订单号不能同时为空
		//model.setOperationId("20171201317348823902"); //支付宝的授权资金操作流水号，冻结成功同步返回
		model.setOutRequestNo("crn15447743161551307");//商户的授权资金操作流水号，与支付宝的授权资金操作流水号不能同时为空	
		request.setBizModel(model);
		
		AlipayFundAuthOperationDetailQueryResponse response = alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	/**
	 * 测试扫码预授权生成授权码
	 * @throws AlipayApiException
	 */
	public static void fundAuthOrderVoucherCreate() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayFundAuthOrderVoucherCreateRequest  request  =  new  AlipayFundAuthOrderVoucherCreateRequest();
		
		AlipayFundAuthOrderVoucherCreateModel  model  =  new  AlipayFundAuthOrderVoucherCreateModel();
		model.setOutOrderNo(getOutOrderNo());  //  预授权冻结交易商户订单号，商户系统唯一标识一笔预授权
		model.setOutRequestNo(getOutRequestNo());  //  商户授权资金操作流水号，不能重复
		model.setOrderTitle("酒店预授权冻结测试");  //  预授权标题,会在支付宝账单中显示
		model.setAmount("0.1");  //  预授权金额，注意需要大于等于结算支付金额
		model.setPayeeLogonId(alipayLogonId);
		model.setPayeeUserId(merchantid);  //  收款方的支付宝唯一用户号,以2088开头的16位纯数字组成，收款方支付宝登录号(payee_logon_id)和用户号(payee_user_id)不能同时为空。
		model.setPayTimeout("5m");  //  预授权冻结超时时间，超时则自动关闭冻结交易
		model.setProductCode("PRE_AUTH");  //  填写固定值PRE_AUTH	
		
		request.setBizModel(model);
		AlipayFundAuthOrderVoucherCreateResponse  response  =  alipayClient.execute(request);
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	/**
	 * 测试预授权冻结
	 * @throws AlipayApiException
	 */
	public static void fundAuthOrderFreeze() throws AlipayApiException{
		AlipayClient alipayClient = new DefaultAlipayClient(url,appid,merchantPrivate,"json","utf-8",alipayPublic,"RSA2");
		
		AlipayFundAuthOrderFreezeRequest  request  =  new  AlipayFundAuthOrderFreezeRequest();
		AlipayFundAuthOrderFreezeModel  model  =  new  AlipayFundAuthOrderFreezeModel();
		model.setAuthCode("2839999997473519824");  //  填写用户支付宝钱包中的付款码
		model.setAuthCodeType("bar_code");  //  填写固定值bar_code
		model.setOutOrderNo(getOutOrderNo());  //  预授权冻结交易商户订单号，商户系统唯一标识一笔预授权
		model.setOutRequestNo(getOutRequestNo());
		model.setOrderTitle("酒店信用预授权冻结测试");  //  预授权标题,会在支付宝账单中显示
		model.setAmount("0.02");  //  预授权金额，注意需要大于等于结算支付金额
		model.setPayeeUserId(merchantid);  //  收款方的支付宝唯一用户号,以2088开头的16位纯数字组成，收款方支付宝登录号(payee_logon_id)和用户号(payee_user_id)不能同时为空。
		model.setPayTimeout("5m");  //  预授权冻结超时时间，超时则自动关闭冻结交易
		model.setProductCode("PRE_AUTH");  //  填写固定值PRE_AUTH	
		request.setBizModel(model);
		request.setNotifyUrl("https://www.tuduoduo.com/notify.htm");
		AlipayFundAuthOrderFreezeResponse  response  =  alipayClient.execute(request);  //需要先创建DefaultAlipayClient类型对象alipayclient
		
		System.out.println("response:  {}"+response.getBody());
	}
	
	public static String getUnFreezeNo(){
		return "uf"+getGuid();
	}
	
	public static String getRefundNo(){
		return "refund"+getGuid();
	}
	
	public static String getPayTradeNo(){
		return "pay"+getGuid();
	}
	
	public static String getOutOrderNo(){
		return "jp"+getGuid();
	}
	
	public static String getOutRequestNo(){
		return "qp"+getGuid();
	}
	
	private static final Random RANDOM = new Random();// 用于GUID生成
	public static String getGuid() {
		StringBuffer buf = new StringBuffer();
		long mills = System.currentTimeMillis() % 100000000000l;
		long seed = Math.abs(RANDOM.nextInt()) % 10000;
		long seed2 = Math.abs(RANDOM.nextInt()) % 10000;
		while (seed2 == seed) {
			seed2 = Math.abs(RANDOM.nextInt()) % 10000;
		}
		seed = seed > 9 ? seed > 99 ? (seed > 999 ? seed : seed * 10)
				: seed * 100 : seed > 0 ? seed * 10 : 9999;
		seed2 = seed2 % 100;
		seed2 = seed2 > 9 ? seed2 : seed2 > 0 ? seed2 * 10 : 99;
		buf.append(mills).append(seed).append(seed2);
		return buf.toString();
	}
}
